package org.liujing.jsw.client.ui;

import com.google.gwt.user.client.*;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.event.logical.shared.*;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;
import java.util.logging.*;

public class BrowserRootPanel implements ResizeHandler{
	private static Logger log = Logger.getLogger("client.BrowserRootPanel");
	private ResizeTimer resizeTimer = new ResizeTimer();
	private static BrowserRootPanel instance = null;
	
	private Widget container;
	
	private BrowserRootPanel(){
		Window.addResizeHandler(this);
		//com.google.gwt.dom.client.Document.get().getBody().setPropertyString("scroll", "no");
		com.google.gwt.dom.client.Document.get().getBody().getStyle().setOverflow(Style.Overflow.HIDDEN);
	}
	
	public void setContent(Widget c){
		Widget oldCon = container;
		container = c;
		RootPanel rp = RootPanel.get();
		if(oldCon != null)
			rp.remove(oldCon);
		rp.add(c);
		//c.getDomElement().getStyle().setOverflow(Style.Overflow.HIDDEN);
		DeferredCommand.addCommand(new DelayedResizeCommand(container));
	}
	
	public void onResize(ResizeEvent event){
		if(container != null){
			resizeTimer.cancel();
			resizeTimer.setEvent(event);
			resizeTimer.schedule(500);
		}
    }
    
    class ResizeTimer extends Timer{
    	ResizeEvent event;
    	
    	public void setEvent(ResizeEvent event){
    		this.event = event;
    	}
    	
    	public void run(){
    		//DeferredCommand.addCommand(new DelayedResizeCommand(container, event.getWidth()  , event.getHeight()));
    		DeferredCommand.addCommand(new DelayedResizeCommand(container));
    	}
    }
	
	public static synchronized BrowserRootPanel get(){
		if(instance == null){
			instance = new BrowserRootPanel();
		}
		return instance;
	}
	
	class DelayedResizeCommand implements Command{
  	  Widget c;
  	  int width = -1;
  	  int height = -1;
  	  public DelayedResizeCommand(Widget target, int w, int h){
  	  	  c = target;
  	  	  width = w;
  	  	  height = h;
  	  }
  	  
  	  public DelayedResizeCommand(Widget target){
  	  	  c = target;
  	  }
  	  
  	  public void execute () {
  	  	if(width < 0)
  	  		width = Window.getClientWidth() ;
  	  	if(height < 0)
  	  		height = Window.getClientHeight();
  	  	GWT.log("execute() resize to :" + width + "," + height);
  	  	//c.setPreferredSize(new Dimension(width, height));
  	  	if(c instanceof JComponent){
  	  		JComponent jc = (JComponent)c;
			jc.setSize(width, height);
			//c.revalidate();
			jc.invalidate();
			RepaintManager.addInvalidComponent(jc);
		}else{
			log.info(width + "px "+ height + "px");
			c.getElement().getStyle().setPosition(Style.Position.ABSOLUTE );
			c.setSize(width + "px", height + "px");
			if(c instanceof RequiresResize)
				((RequiresResize)c).onResize();
		}
		//GWT.log(" html: "+ Document.get().getBody().getInnerHTML());
	 } 
  }
}
